class Event < ActiveRecord::Base

  has_and_belongs_to_many :people
  has_and_belongs_to_many :visitors

  validates_presence_of :name, :starts_at, :ends_at
  
  def self.stop_all
    @running_events = Event.find(:all, :conditions => {:is_current => true})
    @running_events.each do |re|
      re.stop_event
    end
  end
  
  def start_event
    self.is_current = true
    self.save
  end

  def stop_event
    self.is_current = false
    self.save
  end

  def calendar_actions
    title = "<a href=''>#{self.name}</a>"
    #if current_user.is_keyholder?
    # title += "<br /><a href=''>Start Event</a>"
    #end
    return title
  end

  def completed
    if self.ends_at < Time.now
      return true
    else
      return false
    end
  end

  def has_required_volunteers
    volunteers = 0
    self.people.each do |person|
      if person.is_volunteer?
        volunteers += 1
      end
    end
    if volunteers < self.volunteers_req
      return false
    else
      return true
    end
  end

  def has_required_boardmembers
    boardmembers = 0
    self.people.each do |person|
      if person.is_boardmember?
        boardmembers += 1
      end
    end
    if boardmembers < self.boardmembers_req
      return false
    else
      return true
    end
  end

  def has_required_keyholders
    keyholders = 0
    self.people.each do |person|
      if person.is_keyholder
        keyholders += 1
      end
    end
    if keyholders < self.keyholders_req
      return false
    else
      return true
    end
  end

  def visitor_count
    return self.visitors.size
  end
  
end
